require_dependency "rails_generator/base"
require_dependency "rails_generator/lookup"

# here only to make the class instantiable in the console
require_dependency "rails_generator"

class AuthenticatorPcrGenerator < Rails::Generator::NamedBase
	self.spec = Rails::Generator::Spec.new  File.basename(__FILE__), __FILE__, nil

	attr_reader	:session_class_path,
							:session_file_path,
							:session_class_basename,
							:session_file_name,
							:session_class_name,
							:controller_class_path,
							:controller_file_path,
							:controller_class_basename,
							:controller_file_name,
							:controller_class_name,
							:model_file_path,
							:model_class_basename,
							:model_file_name,
							:model_class_name,
							:table_name

	alias_method :model_class_path, :controller_class_path

	def initialize(runtime_args, runtime_opts = {})
		super
		# read arguments
		@session_name = (args.shift || "sessions").pluralize
		@user_name = @name.pluralize

		# session
		basename, @session_class_path, @session_file_path, @session_class_nesting, @session_class_nesting_depth = extract_modules @session_name
		@session_class_basename, @session_file_name, = inflect_names basename
		@session_class_name = @session_file_path.map{|x| x.camelize}.join("::")

		# user
		basename, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules @user_name
		@controller_class_basename, @controller_file_name, = inflect_names basename
		@controller_class_name = @controller_file_path.map{|x| x.camelize}.join("::")
		@model_file_path = @controller_file_path.singularize
		@model_class_name = @controller_class_name.singularize
		@model_file_name = @controller_file_name.singularize
		@model_class_basename = @controller_class_basename.singularize
		@table_name = @controller_class_basename.underscore
	end


  def manifest
    record do |m|
			# check collisions
			m.class_collisions	@session_class_path,
													"#{session_class_name}Controller",
													"#{session_class_name}Helper"
			m.class_collisions	@controller_class_path,
													"#{controller_class_name}Controller",
													"#{controller_class_name}Helper",
													@model_class_name

			# create directories
			m.directory File.join("app/controllers", session_class_path)
			m.directory File.join("app/controllers", controller_class_path)
			m.directory File.join("app/models", model_class_path)
			m.directory File.join("app/views", session_class_path, session_file_name)
			m.directory File.join("app/views", controller_class_path, controller_file_name)

			# copy model
			m.template	'user.rb',
									File.join('app/models', model_class_path, "#{model_file_name}.rb"),
									:assigns => {	:class_basename => model_class_basename,
																:modules => model_class_path.map{|x| x.camelize} }

			# copy controllers
			m.template	'users_controller.rb',
									File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb"),
									:assigns => {	:class_basename => controller_class_basename,
																:modules => controller_class_path.map{|x| x.camelize} }
			m.template	'sessions_controller.rb',
									File.join('app/controllers', session_class_path, "#{session_file_name}_controller.rb"),
									:assigns => {	:class_basename => session_class_basename,
																:modules => session_class_path.map{|x| x.camelize} }
			# copy views
			m.template	"signup.html.erb",
									File.join("app/views", controller_file_path, "signup.html.erb")
			m.template	"edit.html.erb",
									File.join("app/views", controller_file_path, "edit.html.erb")
			m.template	"form.html.erb",
									File.join("app/views", controller_file_path, "_form.html.erb")
			m.template	"index.html.erb",
									File.join("app/views", controller_file_path, "index.html.erb")
			m.template	"show.html.erb",
									File.join("app/views", controller_file_path, "show.html.erb")
			m.template	"new.html.erb",
									File.join("app/views", session_file_path, "new.html.erb")
			# copy library
			m.template	'authenticator.rb',
									File.join('lib', 'authenticator.rb')

			# copy migration
=begin
			m.migration_template	'migration.rb',
														'db/migrate',
														:assigns => { :migration_name => "CreateAuthenticator#{controller_class_basename}" },
														:migration_file_name => "create_authenticator_#{table_name}"
=end
    end
  end
protected
	#override with your own banner
	def banner
		"Usage: #{0} authenticator_pcr UserModelName [SessionControllerName='sessions'] [--use-https]=false"
	end

	def add_options!(opt)
		opt.separator ''
		opt.separator 'Options'
		opt.on(	"--use-https",
						"Use https protocol for actions that require login") {|v| options[:use_https] = v}
	end
end
